{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据处理过程可视化\n",
    "\n",
    "本笔记本展示数据处理全过程的可视化分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_296492/2175810678.py:8: MatplotlibDeprecationWarning: The seaborn styles shipped by Matplotlib are deprecated since 3.6, as they no longer correspond to the styles shipped by seaborn. However, they will remain available as 'seaborn-v0_8-<style>'. Alternatively, directly use the seaborn API instead.\n",
      "  plt.style.use('seaborn')\n"
     ]
    }
   ],
   "source": [
    "# 导入必要的库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# 设置可视化样式\n",
    "import seaborn as sns\n",
    "sns.set_theme(style='whitegrid')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 数据加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载数据\n",
    "data = pd.read_csv('tf-model-development/data/completion_data.csv')\n",
    "\n",
    "# 显示数据前5行\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 数据概览可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据分布可视化\n",
    "plt.figure(figsize=(12, 6))\n",
    "sns.histplot(data['completion_rate'], bins=20, kde=True)\n",
    "plt.title('完成率分布')\n",
    "plt.xlabel('完成率')\n",
    "plt.ylabel('频数')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 特征分析可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 星期几与完成率的关系\n",
    "plt.figure(figsize=(10, 6))\n",
    "sns.boxplot(x='day_of_week', y='completion_rate', data=data)\n",
    "plt.title('星期几与完成率的关系')\n",
    "plt.xlabel('星期几 (1=周一, 7=周日)')\n",
    "plt.ylabel('完成率')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 相关性分析可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算并可视化相关性矩阵\n",
    "plt.figure(figsize=(10, 8))\n",
    "corr = data.drop('date', axis=1).corr()\n",
    "sns.heatmap(corr, annot=True, cmap='coolwarm', fmt='.2f')\n",
    "plt.title('特征相关性矩阵')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 数据处理过程可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 标准化前后的数据分布对比\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 选择数值特征\n",
    "numeric_features = ['planned_tasks']\n",
    "\n",
    "# 标准化前\n",
    "plt.figure(figsize=(12, 5))\n",
    "plt.subplot(1, 2, 1)\n",
    "sns.histplot(data['planned_tasks'], bins=20, kde=True)\n",
    "plt.title('标准化前的计划任务数量分布')\n",
    "\n",
    "# 标准化后\n",
    "scaler = StandardScaler()\n",
    "scaled_data = scaler.fit_transform(data[numeric_features])\n",
    "plt.subplot(1, 2, 2)\n",
    "sns.histplot(scaled_data, bins=20, kde=True)\n",
    "plt.title('标准化后的计划任务数量分布')\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. 最终数据准备可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 特征与目标变量的关系\n",
    "plt.figure(figsize=(15, 10))\n",
    "\n",
    "# 星期几\n",
    "plt.subplot(2, 2, 1)\n",
    "sns.boxplot(x='day_of_week', y='completion_rate', data=data)\n",
    "plt.title('星期几 vs 完成率')\n",
    "\n",
    "# 是否假日\n",
    "plt.subplot(2, 2, 2)\n",
    "sns.boxplot(x='is_holiday', y='completion_rate', data=data)\n",
    "plt.title('是否假日 vs 完成率')\n",
    "\n",
    "# 计划任务数量\n",
    "plt.subplot(2, 2, 3)\n",
    "sns.scatterplot(x='planned_tasks', y='completion_rate', data=data)\n",
    "plt.title('计划任务数量 vs 完成率')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}